package com.chenxingnet.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenxingnet.dto.After;
import com.chenxingnet.service.AfterService;

@Controller
@RequestMapping("/after")
public class AfterController {
	
	@Resource
	private AfterService afterService;
	
	/**
	 * 转至新增页面
	 * @return
	 */
	@RequestMapping("/insertAfter")
	public String insertAfter(HttpServletRequest request,Model model){
		
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		
		return "after-sales_edit";
	}
	/**
	 * 分页查询所有数据
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showAfter")
	public String showAfter(HttpServletRequest request,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if(deptid == 1 && (rolename.equals("业务员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "city";
			}
			if(deptid == 2 && (rolename.equals("业务员"))){
				model.addAttribute("result", "对不起，您无此操作权限");
				model.addAttribute("deptid", deptid);
				return "aaa_city";
			}
		} catch (Exception e) {
			
		}
		
		afterService.selectAllByCount(request, model);
		return "add_after-sales";
	}
	
	/**
	* @Title: /after/mobile_showAfter 
	*移动端售后查询请求
	 */
	@RequestMapping("/mobile_showAfter")
	public void mobile_showAfter(HttpServletRequest request,Model model,HttpServletResponse response){
		
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			
			try {
				String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
				if(rolename.equals("会计")){
					returnJson.put("result", "对不起，您无此操作权限");
				}
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
				if(deptid == 1 && (rolename.equals("业务员"))){
					returnJson.put("result", "对不起，您无此操作权限");
					returnJson.put("deptid", deptid);
				}
				if(deptid == 2 && (rolename.equals("业务员"))){
					returnJson.put("result", "对不起，您无此操作权限");
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			
			afterService.mobSelectAllByCount(request);
			returnJson.put("afterList", request.getAttribute("afterList"));
			returnJson.put("page", request.getAttribute("mobilePage"));
			
			response.getWriter().print(returnJson.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	/**
	 * 新增售后信息
	 * @return
	 */
	@RequestMapping("/addAfter")
	public String addAfter(After after,HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		
		after.setApubdate(new Date());
		after.setAreportstart("1");
		
		afterService.addAfter(after);
		
		return "redirect:/after/showAfter?pageNow=1";
	}
	
	/**
	* @Title: /after/mobile_addAfter 
	*移动端新增售后信息
	 */
	@RequestMapping("/mobile_addAfter")
	public void mobile_addAfter(After after,HttpServletRequest request,Model model,HttpServletResponse response){
		try {
			
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			
			after.setApubdate(new Date());
			after.setAreportstart("1");
			
			int resu = afterService.addAfter(after);
			returnJson.put("result",resu);//返回插入结果。
			
			response.getWriter().print(returnJson.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	
	
}
